#include "g_local.h"
#include "../cola/cola_napi.h"

typedef struct
{
	qboolean live;
	gclient_t *client;
} gColaClient_t;

static gColaClient_t *G_ClientFromObject(void *p)
{
	colaExecObjectRef_t obj;
	if(!p)
		return NULL;

	obj = p;

	return COLA_OBJECTFIELD(obj, 0, gColaClient_t);
}

// ********************************************************
// Utility functions
void G_LinkClientToObject(gclient_t *client, void *obj)
{
	gColaClient_t *cclient;

	client->colaClient = obj;
	cclient = G_ClientFromObject(obj);
	cclient->client = client;
}



// ********************************************************
// Cola API functions
void G_ColaClientGTSA(colaUInt_t *size, colaUInt_t *alignment)
{
	*size = sizeof(gColaClient_t);
	*alignment = COLA_POINTER_ALIGNMENT;
}

void G_ColaClientDisconnect( void *op )
{
	gColaClient_t *cclient;

	cclient = G_ClientFromObject(op);
}

COLA_INLINE colaExecStatus_t colaClientCreate(colaThread_t *thread, colaExecObjectRef_t *objp)
{
	gColaClient_t *cclient;

	cclient = G_ClientFromObject(*objp);

	cclient->live = qtrue;

	return COLA_EXEC_OK;
}

COLA_INLINE colaExecStatus_t colaClientName(colaThread_t *thread, colaString_t *rv, colaExecObjectRef_t *cl)
{
	gColaClient_t *cclient;

	COLA_ASSERT_REF(*cl);

	cclient = G_ClientFromObject(*cl);
	COLA_ASSERT_GENERAL(cclient->live, "Name requested from disconnected client");

	COLA_ASSERT_CALL(colaNTCAToString(thread, rv, cclient->client->pers.netname));

	return COLA_EXEC_OK;
}

COLA_INLINE colaExecStatus_t colaClientAttachInfoString(colaThread_t *thread, colaInteger_t *rv, colaExecObjectRef_t *cl, colaString_t *className)
{
	return COLA_EXEC_OK;
}

#include "g_cola_gameclient.h"

colaAPI_t *G_ColaGameClientAPI( void )
{
	return &colaStubAPI;
}
